package com.zou.practice.rpc;

import lombok.Data;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * Rpc调用客户端
 */
@Data
public class RpcClient {
    private String serverAddress;
    private int serverPort;

    public RpcClient(String serverAddress, int serverPort) {
        this.serverAddress = serverAddress;
        this.serverPort = serverPort;
    }

    public Object sendAndReceive(TransportMessage transportMessage){
        Object result = null;
        try {
            Socket socket = new Socket(serverAddress,serverPort);
            ObjectOutputStream objectOutpusStream = new ObjectOutputStream(socket.getOutputStream());
            objectOutpusStream.writeObject(transportMessage);

            ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
            result = objectInputStream.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return result;
    }

}